package com.example.llt_demo.controller;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONUtil;
import com.example.llt_demo.mybatis.entity.UserPo;
import com.example.llt_demo.mybatis.service.UserService;
import com.example.llt_demo.util.RedisUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequiredArgsConstructor
public class DemoController {
    private final UserService userService;
    private final RedisUtils redisUtils;

    @RequestMapping("/dbtest")
    public ResponseEntity<String> dbtest() {
        UserPo userPo = new UserPo();
        userPo.setId(100);
        userPo.setUsername("test");
        userPo.setPassword("123456");
        userService.save(userPo);

        UserPo getuser = userService.getById(100);

        return new ResponseEntity<>(JSONUtil.toJsonStr(getuser), HttpStatus.OK);
    }

    @RequestMapping("/callMicService")
    public ResponseEntity<String> microservice() {
        // 模拟微服务的调用，后续会通过mockservice模拟这个地址
        String ret = HttpUtil.get("http://127.0.0.1:9093/microservice");
        return new ResponseEntity<>(ret, HttpStatus.OK);
    }

    @RequestMapping("/redis")
    public ResponseEntity<String> redisTest() {
        redisUtils.set("test_key", "redis_test_value");
        String ret = redisUtils.get("test_key");
        return new ResponseEntity<>(ret, HttpStatus.OK);
    }

    @RequestMapping("/mockit")
    public ResponseEntity<String> mockit() {
        return new ResponseEntity<>(mockitTest(), HttpStatus.OK);
    }

    private String mockitTest() {
        return "mock test";
    }
}
